/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package transposematrix;

import java.io.*;
/**
 *
 * @author hhl
 */
public class TransposeMatrix {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        //输入
        int m=0;
        int n=0;
        String s = "";
        System.out.print("请输入矩阵有多少行：");
        try
        {    	
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	    s=in.readLine();
        }
        catch(IOException e){}
        m= Integer.parseInt(s);
        System.out.print("请输入矩阵有多少列：");
        try
        {    	
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	    s=in.readLine();
        }
        catch(IOException e){}
        n= Integer.parseInt(s);
        
        //创建矩阵
        int array[][] = new int[m][n];
        
        //输入矩阵
        System.out.println("请逐一输入行列式的值：从左到右，从上到下：");
        
        int tempElement=0;
        for(int a=0;a<m;a++){
            for(int b=0;b<n;b++){
                try
                {    	
                    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                    s=in.readLine();
                    array[a][b] = Integer.parseInt(s);
                    //tempElement = Integer.parseInt(s);
                }
                catch(Exception e){
                e.printStackTrace();
                System.out.println("小朋友，你输了两个回车，请重新输入该元素：");
                array[a][b] = 0;
                --b;
                continue;
                }
            }
        }

        System.out.println("行列互调前：");
        // 输出二维数组
        printArray(array);
        int array2[][] = new int[array[0].length][array.length];
        for (int i = 0; i < array.length; i++) {// 调整数组行列数据
            for (int j = 0; j < array[i].length; j++) {
                array2[j][i] = array[i][j];
            }
        }
        System.out.println("行列互调后：");
        // 输出行列互调后的二维数组
        printArray(array2);
    }
    private static void printArray(int[][] array) {
        for (int i = 0; i < array.length; i++) {// 遍历数组
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j] + " ");// 不换行输出数组元素
            }
            System.out.println();	//换行
        }
    }
}
 